/**
 * 
 */
package base.servlet;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuboen
 * 
 * @since 2012-7-5
 * @version 1.0.0
 */
public class DownloadFile {

	/**
	 * <DL><DT>WEB下载文本文件</DT></DL>
	 * 
	 * @author liuboen
	 * @param request
	 * @param response
	 * @param contentType
	 * @param fileName
	 * @param content
	 * @throws Exception
	 */
	public static void downloadTextFile(HttpServletRequest request,
			HttpServletResponse response, String contentType, String fileName,
			String content) throws Exception {
		   //String contentType = "application/octet-stream";  
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");

		response.setContentType(contentType);
		response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));

		OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
		BufferedWriter writer = new BufferedWriter(out);

		writer.write(content);

		writer.flush();
		writer.close();
		out.close();
	}
}
